package com.yohann.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.eduservice.entity.EduTeacher;
import com.yohann.eduservice.entity.vo.TeacherQurey;
import com.yohann.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.yohann.commonutils.result.R;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-08-05
 */
@RestController
@RequestMapping("/edu-service/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    /**
     * 查询所有老师
     * @return
     */
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 通过ID删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 条件查询 并分页
     * @param current
     * @param limit
     * @param teacherQurey
     * @return
     */
    @PostMapping("pageQuery/{current}/{limit}")
    public R pageQueryTeacher(@PathVariable long current,
                              @PathVariable long limit,
                              @RequestBody(required = false) TeacherQurey teacherQurey) {
        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacherQurey.getName())) {
            wrapper.like("name", teacherQurey.getName());
        }
        if (!StringUtils.isEmpty(teacherQurey.getLevel())) {
            wrapper.eq("level", teacherQurey.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQurey.getJoinDateBegin())) {
            wrapper.ge("gmt_create", teacherQurey.getJoinDateBegin());
        }
        if (!StringUtils.isEmpty(teacherQurey.getJoinDateEnd())) {
            wrapper.le("gmt_modified", teacherQurey.getJoinDateEnd());
        }

        wrapper.orderByDesc("gmt_create");

        IPage<EduTeacher> teacherPage = teacherService.page(page, wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", teacherPage.getTotal());
        map.put("rows", teacherPage.getRecords());

        return R.ok().data(map);
    }

    /**
     * 添加
     * @param eduTeacher
     * @return
     */
    @PostMapping("add")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);

        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 通过ID查询
     * @param id
     * @return
     */
    @GetMapping("getById/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);

        return R.ok().data("eduTeacher", eduTeacher);
    }

    /**
     * 更新
     * @param eduTeacher
     * @return
     */
    @PostMapping("update")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean update = teacherService.updateById(eduTeacher);

        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}